package com.wz.project.scj.ktp.service.impl;

import com.wz.common.bas.taskNoUtils.service.IBasTaskNoService;
import com.wz.project.bas.basJobId.domain.BasJobId;
import com.wz.project.bas.basJobId.mapper.BasJobIdMapper;
import com.wz.project.bas.inoroutType.mapper.BasInOrOutTypeMapper;
import com.wz.project.mydo.domain.MydoJobDetail;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.scj.ktp.mapper.KTPinMapper;
import com.wz.project.scj.ktp.service.KTPinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * created by qxq on 2021/11/3  11:23
 **/
@Service
public class KTPinServiceImpl implements KTPinService {

    @Autowired
    private KTPinMapper ktPinMapper;

    @Autowired
    private IBasTaskNoService iBasTaskNoService;

    @Autowired
    private BasJobIdMapper basJobIdMapper;

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private BasInOrOutTypeMapper inOrOutTypeMapper;


    @Override
    public Boolean KTPinJob(String trayCode) {

        int num = ktPinMapper.selectJobByTraycode(trayCode);
        if(num>0){
            return false;
        }
        int num1=ktPinMapper.selectStockByTraycode(trayCode);
        if(num1>0){
            return  false;
        }

        String jobId = getJobId("RP");

//        获取任务号
        long ry = iBasTaskNoService.getTaskNo("3");

        MydoJob mydoJob = new MydoJob();
        MydoJobDetail mydoJobDetail = new MydoJobDetail();
        String inorout = inOrOutTypeMapper.selectById("2");
        mydoJob.setInOrOut(inorout);
        mydoJob.setJobId(jobId);
        mydoJob.setExecutionPhase("0");
        mydoJob.setTrayCode(trayCode);
        mydoJob.setStatus("1");
        mydoJob.setOnLineType("1");
//        mydoJob.setEntranceId("1");
        mydoJob.setPriority(10);
//        mydoJob.setClassId(classNo);
        mydoJob.setTaskNo(String.valueOf(ry));
        mydoJob.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        // TODO: 2021/11/3 mydo_job信息完善 ;

        mydoJobDetail.setJobId(jobId);
        mydoJobDetail.setJobDetailId(jobId + "-1");
        //用作托盘类型区分
        mydoJobDetail.setLotNo("KTP0001");
        mydoJobDetail.setInQuantity(new BigDecimal(1));
        mydoJobDetail.setProductId("KTP0001");

        // TODO: 2021/11/3 mydo_job_detail信息完善 ;
        Integer integer = ktPinMapper.insretNewJob(mydoJob);
        Integer integer1 = ktPinMapper.insretNewJobDetail(mydoJobDetail);

        if (integer > 0 && integer1 > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean KTPoutJob(String quantity, String type,String exit) {
        boolean a=true;
        for (int i = 0; i < Integer.valueOf(quantity); i++) {
            StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_auto_out_tp");
            StoredProcedureQuery parameter = storedProcedureQuery

                    .registerStoredProcedureParameter("v_type", String.class, ParameterMode.IN)
                    .registerStoredProcedureParameter("v_exit", String.class, ParameterMode.IN)
                    .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

            parameter.setParameter("v_type", type);
            parameter.setParameter("v_exit", exit);
            //执行
            storedProcedureQuery.execute();
            //获取返回结果
            String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");
            if ("10".equals(result)) {
                a= true;
                System.out.println("");
            } else {
                a= false;
            }
        }
        return a;
    }

    public String getJobId(String type) {


        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(new Date());
        int count = basJobIdMapper.getIdByDate(type, date);
        String jobId = "";
        if (count > 0) {
            String jobIdPre = basJobIdMapper.getId(type);
            String[] split = jobIdPre.split(date);
            int i = Integer.parseInt(split[1]);
            if (i > 9999) {
                i = 1;
            }
            String num = String.format("%04d", ++i);
            jobId = type + date + num;
            BasJobId basJobId = new BasJobId();
            basJobId.setJobId(jobId);
            basJobId.setType(type);
            basJobIdMapper.updateBasJobId(basJobId);
        } else {
            int i = 1;
            String num = String.format("%04d", i);
            jobId = type + date + num;
            BasJobId basJobId = new BasJobId();
            basJobId.setJobId(jobId);
            basJobId.setType(type);
            basJobIdMapper.updateBasJobId(basJobId);

        }
        return jobId;
    }
}
